package com.welinkdata;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;

/**
 * Created by Administrator on 2016/8/25.
 */
public class DemoHttpClient {

    public static void main(String[] args) throws IOException {
        String url = "http://www.welinkdata.com/";
//        demo1(url);
//        demo2(url);
        demo3(url);
    }

    //1. httpclient
    //POST
    public static void demo1(String url) throws IOException {
        HttpClient httpClient = HttpClients.custom().build();
        HttpGet get = new HttpGet(url);
        HttpResponse httpResponse =  httpClient.execute(get);
        String html  = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
        System.out.println(html);
    }

    /**
     * jsoup
     * @param url
     * @throws IOException
     */
    public static void demo2(String url) throws IOException {
        Connection connection = Jsoup.connect(url);
        Connection.Response response = connection.execute();
        System.out.println(response.body());
    }

    /**
     * htmlunit
     * @param url
     */
    public static void demo3(String url) throws IOException {
        WebClient webClient =  new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(false);
        HtmlPage htmlPage = webClient.getPage(url);
        System.out.println(htmlPage.asXml());
        webClient.close();
    }

}
